﻿#include "trainingreport.h"
#include "ui_trainingreport.h"
#include <QPainter>
#include "cdatabaseinterface.h"
#include <QDebug>
#include "currentuserdata.h"
#include <QGraphicsView>
#include "userdataformate.h"
TrainingReport::TrainingReport(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TrainingReport),
    m_chart(NULL),
    m_chartview(NULL),
    m_axisX(NULL),
    m_axisY(NULL)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::Dialog|Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground,true);    //设置透明
    m_axisX=new QValueAxis;
    m_axisY=new QValueAxis;
    m_chart=new QChart;
    m_chartview=new QChartView(m_chart);

}

TrainingReport::~TrainingReport()
{
    if(m_chart)
        delete m_chart;
    if(m_chartview)
        delete m_chartview;
    if(m_axisX)
        delete m_axisX;
    if(m_axisY)
        delete m_axisY;
    delete ui;
}

void TrainingReport::on_noSave_pushButton_clicked()
{
    this->close();
}


void TrainingReport::on_save_pushButton_clicked()
{
    //保存图片
    QString fileName;
    fileName.append("./createFile/trainReport/report/");
    fileName.append(m_st_trainReport.reportDate.toString("yyyyMMddhhmmss")+".png");
    if(!ui->graphicsView->grab().save(fileName))
        qDebug()<<"报告图片保存失败";
    else {
        m_st_trainReport.backGroundPixmapPath = fileName;
    }

    //将数据存储到数据库中
    QString tableName("TrainReport");
    if(!CDatabaseInterface::getInstance()->insertRowTable(tableName,trainReportToVariantMap(m_st_trainReport)))
        qDebug()<<"训练报告存储失败"<<CDatabaseInterface::getInstance()->getLastError();
    else
        QMessageBox::warning(NULL,tr("提示"),tr("报告保存成功"));
    //添加训练记录
    QString recordTable("TrainRecord");
    ST_TrainRecord st_trainRecord;
    st_trainRecord.ID = m_st_trainReport.ID;
    st_trainRecord.trainingDate = m_st_trainReport.reportDate;
    st_trainRecord.trainingDuration = m_st_trainReport.trainTime;
    qDebug()<<"m_st_trainReport.trainTime"<<m_st_trainReport.trainTime;
    st_trainRecord.resistance = m_st_trainReport.resistance;

    switch(m_st_trainReport.trainDifficulty)
    {
    case 0:
        st_trainRecord.difficulty = tr("低");
        break;
    case 1:
        st_trainRecord.difficulty = tr("中");
        break;
    case 2:
        st_trainRecord.difficulty = tr("高");
        break;
    }

    st_trainRecord.score = m_st_trainReport.score;
    st_trainRecord.gameplay = m_st_trainReport.gameplay;

    if(!CDatabaseInterface::getInstance()->insertRowTable(recordTable,trainRecordToVariantMap(st_trainRecord)))
        qDebug()<<"训练记录存储失败"<<CDatabaseInterface::getInstance()->getLastError();
    else
        QMessageBox::warning(NULL,tr("提示"),tr("训练记录保存成功"));
    this->close();

    
}

void TrainingReport::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter painter(this);
    painter.fillRect(rect(),QColor(0,0,0,100));
}

void  TrainingReport::setReportData(const ST_TrainReport &st_trainReport,int type,QList<QPointF> &pointList)
{

    ST_PatientMsg st_patientMsg;
    st_patientMsg = CurrentUserData::getInstace()->getCurrentPatientMsg();
    

    m_st_trainReport=st_trainReport;

    ui->name_label->setText(st_patientMsg.userName);
    QString sex = st_patientMsg.sex == 1 ? tr("女") :tr("男");
    ui->sex_label->setText(sex);
    ui->height_label->setText(QString::number(st_patientMsg.height)+tr("cm"));
    ui->phone_label->setText(st_patientMsg.userPhone);
    ui->age_label->setText(QString::number(st_patientMsg.age) + tr("岁"));
    ui->remark_label->setText(st_patientMsg.remarkMessage);
    QDateTime m_dataTime;
    m_dataTime =QDateTime::currentDateTime();
    ui->reportDate_label->setText(m_dataTime.toString("yyyy-MM-dd"));
    qDebug()<<"st_trainReport.trainTime"<<st_trainReport.trainTime;
    int min = st_trainReport.trainTime/60;
    int sec = st_trainReport.trainTime%60;
    ui->trainTime_label->setText(QString("%1 min  %2 s").arg(min).arg(sec));
    ui->resistance_label->setText(QString::number(st_trainReport.resistance)+tr("档"));
    ui->range_label->setText(QString::number(st_trainReport.range)+tr("°"));
    QString difficultStr;
    switch(st_trainReport.trainDifficulty)
    {
    case 0:
        difficultStr = tr("低");
        break;
    case 1:
        difficultStr = tr("中");
        break;
    case 2:
        difficultStr = tr("高");
        break;
    }
    ui->trainingDifficult_label->setText(difficultStr);
    ui->gamePlay_label->setText(st_trainReport.gameplay);


    if(st_trainReport.direction ==1)
    ui->direction_label->setPixmap(QPixmap(":/icon/front.png"));
    ui->direction_label->setScaledContents(true);
    if(st_trainReport.direction ==2)
        ui->direction_label->setPixmap(QPixmap(":/icon/back.png"));
    ui->direction_label->setScaledContents(true);
    if(st_trainReport.direction ==3)
        ui->direction_label->setPixmap(QPixmap(":/icon/left.png"));
    ui->direction_label->setScaledContents(true);
    if(st_trainReport.direction ==4)
        ui->direction_label->setPixmap(QPixmap(":/icon/right.png"));
    ui->direction_label->setScaledContents(true);


    if(1 == type)//生成
    {
        ui->stackedWidget->setCurrentIndex(0);
        trainAngleChart(pointList);
        ui->save_pushButton->setVisible(true);
        ui->noSave_pushButton->setVisible(true);
        ui->confirm_pushButton->setVisible(false);
    }
    else if(2 == type)//加载图片
    {
        ui->save_pushButton->setVisible(false);
        ui->noSave_pushButton->setVisible(false);
        ui->confirm_pushButton->setVisible(true);

        QPixmap pix;
        pix.load(st_trainReport.backGroundPixmapPath);

        ui->report_Label->setPixmap(pix);
        ui->stackedWidget->setCurrentIndex(1);
    }
    this->show();
}

ST_TrainReport TrainingReport::getReportDataByStartTime(QString reportDateStr)
{
    ST_TrainReport st_trainReport;
    QString queryStr(QString("select * from TrainReport where reportDateStr = '%1'").arg(reportDateStr));
    if(CDatabaseInterface::getInstance()->exec(queryStr))
    {
        if(CDatabaseInterface::getInstance()->getValuesSize() > 0)
        {
            st_trainReport = variantMapToTrainReport(CDatabaseInterface::getInstance()->getValues(0,1).at(0));
        }
        else
            qDebug()<<"TrainReport::getReportDataByStartTime:未查到符合要求数据";
    }
    else
        qDebug()<<"TrainReport::getReportDataByStartTime"<<CDatabaseInterface::getInstance()->getLastError();

    return st_trainReport;
}
void TrainingReport::trainAngleChart(const QList<QPointF> &pointList)
{
    m_axisX->setRange(-12, 12);         //设置范围
    m_axisX->setLineVisible(true);      //设置轴线和刻度线可见性
    m_axisX->setGridLineVisible(true); //设置网格线可见性

    m_axisY->setRange(-12, 12);         //设置范围
    m_axisY->setLineVisible(true);      //设置轴线和刻度线可见性
    m_axisY->setGridLineVisible(true); //设置网格线可见性

    m_chart = ui->graphicsView->chart();
    m_chart->removeAllSeries();

    m_chart->addAxis(m_axisX, Qt::AlignBottom);  //将坐标轴加到chart上，居下
    m_chart->addAxis(m_axisY, Qt::AlignLeft);    //居左
    QLineSeries *series = new QLineSeries;

    for(int i=0;i<pointList.size();i++)
    {
        QPointF point;
        point.setX(pointList.at(i).x());
        point.setY(pointList.at(i).y());
        series->append(point);
    }
    //将创建好的折线图series添加进chart中
    m_chart->addSeries(series);

}


void TrainingReport::on_confirm_pushButton_clicked()
{
    this->close();
}

void TrainingReport::changeEvent(QEvent* event)
{
    switch (event->type())
    {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        QWidget::changeEvent(event);
        break;
    }
}
